#
# Package.
#

package iSOCK::Model::Integration;

#
# Parent.
#

use Moose;
use namespace::autoclean;

BEGIN { extends 'iSOCK::Model::Root' }

no Moose;

#
# Dependencies.
#

use SOCK::ADI;
use SOCK::INI::Design;
use SOCK::LOG;
use SOCK::SVN;

use Try::Tiny;

#
# Documentation.
#

=head1 NAME

iSOCK::Model::Integration - Catalyst Model

=head1 DESCRIPTION

Catalyst Model.

=head1 AUTHOR

Xavier Caron

=head1 LICENSE

This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

#
# Functions.
#

sub component_lineup {
  my ( $self, $option ) = @_;
  
  my ( $c, $url, $chip, $technology, $product_id, $dummy ) = map { $option->{$_} } qw( c url chip technology product_id dummy );
  
  my $session_path = $c->session->{session_path} || '/tmp';
  
  if ( my $product_r = $c->model ( 'App::Product' )->find ( $product_id ) ) {
    $url ||= $product_r->integration_scm_url;
    
    if ( $url ) {
      my $log = SOCK::LOG->new ({ file => "$session_path/component_lineup.log" });

      my $svn = SOCK::SVN->new ({ repository_name => 'product' });

      my $wc_path = "$session_path/$product_id";

      system "rm -rf $wc_path" if -d $wc_path;

      my $svn_auth = '';
      if ( my $svn_login = $product_r->integration_scm_login ) {
        $svn_auth = "--non-interactive --username '$svn_login' ";
        
        if ( my $svn_password = $product_r->integration_scm_password ) {
          $svn_auth .= "--password '$svn_password' ";
        }
      }

      my $branch     = $product_r->integration_scm_branch;
      my $config_url = "$url/" . ( $branch ? "branches/$branch" : "trunk" ) . "/CONFIG";

      if ( $svn->svn ({ url => $config_url, subcommand => 'checkout', options => "$svn_auth$wc_path/CONFIG" }) ) {
        my $hoh;

        $chip       ||= $product_r->integration_config_chip_id;
        $technology ||= $product_r->integration_config_technology_id;

        if ( $chip ) {
          try {
            my $ini = SOCK::INI::Design->new ({ path => $wc_path, name => $chip, log => $log });

            $hoh = $ini->as_hoh;
            
            $technology ||= $hoh->{technology};
          }
          catch {
            die "Could not parse '$chip' chip config!";
          };
        }

        if ( $technology ) {
          my $adi_file = "$wc_path/CONFIG/technologies/$technology/atmel_design.ini";
          
          $log->say ( "Parsing '$adi_file' INI file..." );
          push @{$hoh->{components}}, SOCK::ADI->new ({ file => $adi_file })->components;
          $log->say ( "Done." );
        }

        unless ( $dummy ) {
          $product_r->set_column ( integration_scm_url => $url );

          $product_r->update;
        }

        if ( $hoh ) {
          $hoh->{product_id}                       = $product_id;
          $hoh->{product_scope}                    = 'integration';

          $hoh->{integration_scm_url}              = $url;
          $hoh->{integration_scm_branch}           = $branch     if $branch;
          $hoh->{integration_config_chip_id}       = $chip       if $chip;
          $hoh->{integration_config_technology_id} = $technology if $technology;

          return $hoh;
        }
      }
      else {
        die "Could not checkout from '$config_url' URL!";
      }
    }
    else {
      die "No URL to checkout from!";
    }
  }
  else {
    die "Unknown '$product_id' product!";
  }

  return;  
}

#
# True.
#

1;
